package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.enums.EnumItemStatus;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.IItemService;
import com.taotao.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zhangxiaolin@zhonhajt.com
 * @Title: 商品管理业务实现类
 * @Package com.taotao.service.impl
 * @Description:
 * @date 2018/10/9 17:54
 */

@Service
public class ItemServiceImpl implements IItemService {

    @Autowired
    private TbItemMapper itemMapper;

    @Autowired
    private TbItemDescMapper itemDescMapper;

    @Override
    public TbItem getItemById(long itemId) {
        return itemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemExample example = new TbItemExample();
        List<TbItem> itemList = itemMapper.selectByExample(example);

        PageInfo<TbItem> pageInfo = new PageInfo<>(itemList);
        return new EasyUIDataGridResult(pageInfo.getTotal(), itemList);
    }

    @Override
    @Transactional
    public TaotaoResult addItem(TbItem tbItem, String desc) {
        //生产商品id
        long itemId = IDUtils.genItemId();
        tbItem.setId(itemId);
        tbItem.setStatus((byte) EnumItemStatus.NORMAL.getCode());
        tbItem.setUpdated(new Date());
        tbItem.setCreated(new Date());

        this.itemMapper.insert(tbItem);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setUpdated(new Date());
        tbItemDesc.setCreated(new Date());

        this.itemDescMapper.insert(tbItemDesc);
        return TaotaoResult.ok();
    }
}
